using System;

namespace Common.Core.Objects
{
	public class ObjectResolver<ObjectType> : IObjectResolver where ObjectType : class
	{
		readonly IObjectFinder finder;
		readonly IObjectCreator creator;
		public event EventHandler<ObjectResolvedEventArgs<ObjectType>> Resolved = delegate { };

		public ObjectResolver( IObjectFinder finder, IObjectCreator creator )
		{
			this.finder = finder;
			this.creator = creator;
		}

		public ObjectType Resolve( object source )
		{
			var result = (ObjectType)( finder.Find( source ) ?? creator.Create( source ) );
			Resolved( this, new ObjectResolvedEventArgs<ObjectType>( source, result ) );
			return result;
		}

		object IObjectResolver.Resolve( object source )
		{
			var result = Resolve( source );
			return result;
		}
	}
}